package com.example.demo.controller;


import com.example.comon.domain.LoginUser;
import com.example.comon.domain.R;
import com.example.comon.utils.UserUtil;
import com.example.demo.domain.Bo.AddRoleBo;
import com.example.demo.domain.Bo.RoleListBo;
import com.example.demo.domain.Role;
import com.example.demo.domain.Vo.RoleListVo;
import com.example.demo.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.RoleList;
import java.util.List;

@RestController
@RequestMapping("/role")
@Api(value = "role",tags = "角色控制器")
public class RoleController {

    @Autowired
    private IRoleService roleService;


    @GetMapping("getRoleList")
    @ApiOperation("获取角色列表")
    public R<List<RoleListVo>> getRoleList(RoleListBo bo){
        Role role = new Role();
        role.setRoleName(bo.getRoleName());
        List<RoleListVo>list = roleService.selectRole(role);
        return R.success(list);
    }

    @ApiOperation("增加角色")
    @PostMapping("/addRole")
    public R addRole(@RequestBody AddRoleBo bo){
        Role role = new Role();
        role.setRoleName(bo.getRoleName());
        role.setRemark(bo.getRemark());
        LoginUser loginUser = UserUtil.getLoginUser();//获取当前登录账号
        role.setCreateBy(loginUser.getUserId());
        roleService.insertRole(role);
        return R.success();
    }

    @PostMapping("/updateRoleById")
    @ApiOperation("修改角色")
    public R updateRoleById(@RequestBody Role role){
        roleService.updateRoleById(role);
        return R.success();
    }

    @DeleteMapping("/delRoleById")
    @ApiOperation("删除角色")
    public R delRoleById(long roleId){
        roleService.delRoleById(roleId);
        return R.success();
    }
}
